package cn.geminis.workflow.service.entity;

import cn.geminis.lowcode.annotation.*;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.persistence.Column;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Transient;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.util.ObjectUtils;

/**
 * @author Allen
 */
@Setter
@Getter
@UuidEntity(prePersist = false)
@DynamicInsert
@DynamicUpdate
@QueryFilter
@CrudRepository
@CrudController
public class Category {

    @Column(nullable = false, length = 128)
    protected String name;

    @Column(nullable = false)
    private String pathId;

    @Column(length = 36)
    @QueryFilterProperty
    @QueryFilterProperty(filterType = QueryFilterProperty.FilterType.IsNull)
    private String parentId;

    @Column(nullable = false)
    private int showOrder;

    @Transient
    @JsonProperty
    private String parentPath;

    @PrePersist
    void setParent() {
        this.setId();
        if (ObjectUtils.isEmpty(this.parentPath)) {
            throw new RuntimeException("需要设置上级ID路径");
        }
        this.pathId = this.parentPath + this.getId() + "/";
    }
}
